var _uri = 'goods_right_sell_inquiry';
var _uri_query = 'goods_right_sell';
$(function() {
	initDP();
	initTitles();
});

function query(){
	if($.trim($('#begin_time').val()).length>0 && !isDateStr($('#begin_time').val())){
		alert('开始日期有误');$('#begin_time').focus();return;
	}else if($.trim($('#end_time').val()).length>0 && !isDateStr($('#end_time').val())){
		alert('结束日期有误');$('#end_time').focus();return;
	}else if(compareDateStr($('#begin_time').val(), $('#end_time').val())<0){
		alert('结束日期应大于开始日期');$('#end_time').focus();return;
	}
	var datas = {fun:'query'};
	if($('#begin_time').val().length>0 || $('#end_time').val().length>0){
		datas['date_range'] = $('#begin_time').val()+' ~ ' + $('#end_time').val();
		var date1 = str2date($('#begin_time').val());
		date1.setDate(date1.getDate()-365);
		var date2 = str2date($('#end_time').val());
		date2.setDate(date2.getDate()-365);
		datas['prev_date_range'] = date2str(date1,'-')+' ~ '+date2str(date2,'-');
	}
	var oChk = $('input:checkbox');
	var str='';
	for(var i=0;i<oChk.length;i++){
		var chk = oChk.eq(i)[0].checked;
		if(chk==true) str += ((str.length>0?'_###_':'')+oChk.eq(i).val());
	}
	var oIpts = $('input[type="text"]');
	for(var i=0;i<oIpts.length;i++){
		if($.trim(oIpts.eq(i).val()).length>0)
			if(oIpts.eq(i)[0] && $.trim(oIpts.eq(i).val()).length>0)
				datas[oIpts.eq(i)[0].id] = oIpts.eq(i).val();
	}
	if(str.length>0) datas['items'] = str;
	postHandler(_uri, datas, function(val){
		window.location= _uri_query + '.html';
		return false;
	});
}
function initDP(){
	$(".dpicker").datepicker({dateFormat: 'yy/mm/dd',showButtonPanel: true,changeMonth: true, changeYear: true, yearRange: '-10:+0', showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.png'});
	/*
	$('#Custom_Att_4').datepicker({dateFormat: 'yy',stepMonths: 12,showButtonPanel: true,maxDate:0,minDate:-10*365,currentText: '今年',beforeShow:doBeforeShow,onChangeMonthYear:changeMonthYear, changeMonth: true,changeYear: true, yearRange: '-10:+0', showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.png'})
		.focus(function(){$("#ui-datepicker-div table, select.ui-datepicker-month").hide();});
	*/
}
function doBeforeShow(input, inst){
	var height = $(input).height();
	var offset = $(input).offset();
	window.setTimeout(function(){
        inst.dpDiv.css({top:(offset.top+height+4)+'px',left: offset.left+'px'});
    },1);
}
function changeMonthYear(year, month, inst){
	$('#'+inst.id).val(year);
}
function initTitles(){
	postHandler(_uri, {fun:'titles'}, function(val){
		initPopup(val);
		initPopupWindow();
		setTimeout('initChkbox()',500);
	});
}
var popup_counter = 0;
function initPopup(val){
	shop_counter = 0;
	var oWin = $('.checkbox_box_w');
	var html = '<label><input name="checkbox" type="checkbox" id="" checked="checked" value=""><strong style="color:#00aacc;">全部</strong></label><div class="checkbox_box"></div>'+
		'<input class="cancel button_" onclick="chkboxOk()" type="button" value="确定" />';
	oWin.html(html);
	var div = $('.checkbox_box_w .checkbox_box');
	var tok = val.split('_####_');
	var idx = 0;
	if(val.length==0){
		oWin.html('<div>无资料</div>');
	}
	for(var i=0;val.length>0 && i<tok.length;i++){
		var tok2 = tok[i].split('_#_#_');
		var t1 = tok2[0];
		var tok3 = tok2[tok2.length-1].split('_###_');
		var tok4 = tok3[0].split('_##_');
		var _div = $('<div/>');
		var _html = '<label><input type="checkbox" name="checkbox" id="t_'+i+'" checked="checked" value="'+(tok4.length==1?tok4[0]:'')+'"><strong>'+t1+'</strong></label>';
		if(tok2.length>1 && tok4.length>1){
			_html += '<div class="checkbox_box" style="border-left-style: dotted;">';
			for(var j=0;j<tok3.length;j++){
				var tok5 = tok3[j].split('_##_');
				_html += '<div><label><input type="checkbox" name="checkbox" id="t_'+i+'_'+j+'_'+idx+'" checked="checked" value="'+tok5[1]+'">'+tok5[0]+'</label></div>';
				idx++;
				popup_counter ++;
			}
			_html += '</div>';
		}else
			popup_counter ++;
		_div.html(_html).appendTo(div);
	}
}
function chkboxOk(){
	var oTar = $('.checkbox_box_w');
	var clone = oTar.clone();
	var oChks = clone.find('input:checkbox');
	var str='';
	$('.show_select').html('<input type="hidden" id="chks_data" value=""/></div>');
	oTar.find('input:checkbox').each(function(idx){
		var chk = $(this)[0].checked;
		var oDiv = oChks.eq(idx).closest('div');
		if(oDiv.parent().hasClass('checkbox_box_w')) oDiv = oDiv.parent();
		if(chk==false){
			oDiv.remove();
		}else{
			if($(this).val().length>0)
			str += ((str.length>0?',':'')+$(this).val());
			oChks.eq(idx).remove();
		}
		if(idx=oChks.length-1){
			str = str.replace(/,/g,'_###_');
			$('.show_select').html('<div class="checkbox_box_w">'+clone.find('.checkbox_box:eq(0)').html()+'<input type="hidden" id="chks_data" value="'+str+'"/></div>');
			clone.remove();
		}
	});
	
}